import {createSlice} from "@reduxjs/toolkit";
import useSetStorage from "@/hooks/useSetStorage";
import useGetStorage from "@/hooks/useGetStorage";
import {useEffect, useState} from "react";

// @ts-ignore
let token = null
// @ts-ignore
let loginData = null

let userInfo = null

useGetStorage('token').then(res => {
    token = res
})
useGetStorage('loginData').then(res => {
    loginData = res
})

useGetStorage('userInfo').then(res => {
    userInfo = res
})

export const loginSlice = createSlice({
    name: "login",
    initialState: () => {
        return {
            token,
            loginData,
            userInfo,
        };

    },
    reducers: {
        set_token(state, action) {
            state.token = action.payload
            console.log(action.payload);
            useSetStorage('token', action.payload)
        },
        set_loginData(state, action) {
            state.loginData = action.payload
            useSetStorage('loginData', action.payload)
        },
        set_userInfo(state, action) {
            state.userInfo = action.payload
            useSetStorage('userInfo', action.payload)
        },
    }
});

export const {
    set_token,
    set_loginData,
    set_userInfo
} = loginSlice.actions;